package com.kxscs.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kxscs.R;
import com.kxscs.adapter.VoyageAdapter;
import com.kxscs.bean.VoyageBean;
import com.kxscs.bean.VoyageEntity;
import com.kxscs.constant.CacheDataConstant;
import com.kxscs.constant.UrlConstant;
import com.kxscs.netBean.GeoUtil;
import com.kxscs.util.AppHttpUtils;

import java.util.ArrayList;
import java.util.List;

public class VoyageListActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private ListView listView;
    private VoyageAdapter adapter;
    private List<VoyageEntity> entities = new ArrayList<>();
    private int page = 1;
    private int total;
    private TextView startPortTv;
    private TextView destPortTv;
    private String scId = "";
    private String startPort;
    private String destPort;

    @Override
    protected void setView() {
        setContentView(R.layout.activity_voyage_list);
    }
    @Override
    protected void initView(TextView leftView,TextView titleView,TextView rightView) {
        showBufferDialog();
        Intent intent = getIntent();
        scId = intent.getStringExtra(SCID);
        startPort = intent.getStringExtra(STARTPORT);
        destPort = intent.getStringExtra(DESTPORT);
        startPortTv = (TextView) findViewById(R.id.start_port);
        startPortTv.setText(startPort);
        destPortTv = (TextView) findViewById(R.id.dest_port);
        destPortTv.setText(destPort);
        listView = (ListView) findViewById(R.id.activity_search_voyage_list);
        adapter = new VoyageAdapter(this, entities);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }
    @Override
    protected void initData() {
        String url = UrlConstant.getVoyageUrl(page, startPort, destPort, scId);
        AppHttpUtils.getUtils(url,new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what /100==2) {
                    VoyageBean bean = null;
                    try {
                        bean = new GeoUtil().deserializer(msg.obj.toString().trim(),VoyageBean.class);
                        total = bean.getTotalCount();
                    }catch (Exception e){
                        Toast.makeText(VoyageListActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                    }

                    if (total == 0) {
                        emptyView.setImageResource(R.mipmap.no_msg);
                    } else if (bean != null && bean.getResult() != null && bean.getResult().size() > 0) {
                        entities.addAll(bean.getResult());
                        adapter.notifyDataSetChanged();
                        bufferDialog.dismiss();
                    }
                }else {
                    emptyView.setImageResource(R.mipmap.no_msg);
                }
            }
        });
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        CacheDataConstant.voyageEntity = entities.get(i);
        Intent intent = new Intent(this, VoyageDetailActivity.class);
        intent.putExtra(STARTPORT, startPort);
        intent.putExtra(DESTPORT, destPort);
        startActivityForResult(intent, LOCKCODE);
    }
}
